/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cardgame;

import java.awt.Graphics;
import cardgame.game.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.HashMap;

/**
 *
 * @author Carl
 */
public class PanCard extends javax.swing.JPanel{

    public PanCard(){
        initComponents();
        setOpaque(false);
    }
    public static final int TAP_OFFSET = 20;
    private static HashMap<String, Image> imageCache = new HashMap<String, Image>();
    private Card card;
    private boolean isVisibleToUser = true;
    private boolean isHighlighted;
    private static BufferedImage resourceImage_Highlight = Util.getResourceImage("/cardgame/resources/images/highlight.png");
    private Font fontStats = new Font("Tahoma", Font.BOLD, 12);
    
    public void setCard(Card card){
        this.card = card;
        updateUI();
    }

    public void setIsVisibleToUser(boolean isVisibleToUser){
        this.isVisibleToUser = isVisibleToUser;
        updateUI();
    }

    public boolean isVisibleToUser(){
        return isVisibleToUser;
    }

    public void setHighlighted(boolean isHighlighted){
        this.isHighlighted = isHighlighted;
        updateUI();
    }

    public boolean isIsHighlighted(){
        return isHighlighted;
    }
Image createImage;
    @Override
    protected void paintComponent(Graphics _graphics){
        super.paintComponent(_graphics);
        Graphics2D graphics = (Graphics2D) _graphics;
        if(card != null){
            boolean isTapped = ((card != null) && card.isTapped());
            if(isTapped){
                graphics.translate(10, 5);
                graphics.rotate(0.2);
            }
            else{
                graphics.translate(5, 7.5);
            }
            graphics.scale(0.75, 0.75);
            if(isVisibleToUser){
                graphics.setColor(ManaAmount.getColor(card.getManaTypes()[0]));
                graphics.fillRect(0, 0, (getWidth() - 1), (getHeight() - 1));
                if(!(card instanceof SimpleManaCard)){
                    graphics.setColor(Color.WHITE);
                    graphics.fillRect(5, 5, 30, 30);
                }
                String imageResourcePath = PanCardInfo.getCardImageResourcePath(card);
                graphics.drawImage(getCachedImage(imageResourcePath), 5, 5, 30, 30, this);
                if(card instanceof MonsterCard){
                    MonsterCard monsterCard = (MonsterCard) card;
                    graphics.setFont(fontStats);
                    graphics.setColor((card.getManaTypes()[0] == Mana.BLACK)?Color.WHITE:Color.BLACK);
                    graphics.drawString("" + monsterCard.getAttackDamage(), 7, 51);
                    graphics.drawString("/", 17, 51);
                    if(monsterCard.getCurrentLifepoints() != monsterCard.getMaximumLifepoints()){
                        graphics.setColor((card.getManaTypes()[0] == Mana.RED)?Color.BLUE:Color.RED);
                    }
                    graphics.drawString("" + monsterCard.getCurrentLifepoints(), 25, 51);
                    graphics.setColor(Color.BLACK);
                    graphics.drawRect(5, 5, 29, 29);
                }
            }
            else{
                graphics.drawImage(PanCard_Deck.resourceImage_Back, 0, 0, (getWidth() - 1), (getHeight() - 1), this);
            }
            if(isHighlighted){
                graphics.drawImage(resourceImage_Highlight, 0, 0, (getWidth() - 1), (getHeight() - 1), this);
            }
            graphics.setColor(Color.BLACK);
            graphics.drawRect(0, 0, (getWidth() - 1), (getHeight() - 1));
        }
    }
    
    public static Image getCachedImage(String resourcePath){
        Image image = imageCache.get(resourcePath);
        if(image == null){
            image = Toolkit.getDefaultToolkit().createImage(Util.getResourceURL(resourcePath));
            imageCache.put(resourcePath, image);
        }
        return image;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 40, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 60, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
